package member

import (
	"context"
	"errors"
	"group-buy/group_buy/internal/common"
	"group-buy/group_buy/internal/models"
	"group-buy/group_buy/internal/request_models"
	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type BindCardLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBindCardLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindCardLogic {
	return &BindCardLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BindCardLogic) BindCard(req *types.CardInfo) (resp *types.CardInfo, err error) {
	member := &models.Member{Id: req.UserId}
	get, err := l.svcCtx.Engine.Get(member)
	if err != nil {
		return nil, err
	}
	if !get {
		return nil, errors.New("会员查询失败")
	}
	if len(member.LsUser) == 0 {
		return nil, errors.New("请先完成实名认证")
	}
	if len(member.LsCard) > 0 {
		return nil, errors.New("您已绑卡成功，可以继续体验其他功能")
	}

	cardInfo := &models.LsCardInfo{
		Id:               req.Id,
		UserId:           req.UserId,
		CardType:         req.CardType,
		CardName:         req.CardName,
		CardNo:           req.CardNo,
		ProvId:           req.ProvId,
		AreaId:           req.AreaId,
		BranchCode:       req.BranchCode,
		BranchName:       req.BranchName,
		CertNo:           req.CertNo,
		CertValidityType: req.CertValidityType,
		CertBeginDate:    req.CertBeginDate,
		CertEndDate:      req.CertEndDate,
		Mp:               req.Mp,
		CardId:           req.CardId,
	}
	lsCard := &request_models.LsCardInfo{
		CardType:         cardInfo.CardType,
		CardName:         cardInfo.CardName,
		CardNo:           cardInfo.CardNo,
		ProvId:           cardInfo.ProvId,
		AreaId:           cardInfo.AreaId,
		BankCode:         req.BankCode,
		BranchCode:       cardInfo.BranchCode,
		BranchName:       cardInfo.BranchName,
		CertType:         req.CertType,
		CertNo:           cardInfo.CertNo,
		CertValidityType: cardInfo.CertValidityType,
		CertBeginDate:    cardInfo.CertBeginDate,
		CertEndDate:      cardInfo.CertEndDate,
		Mp:               cardInfo.Mp,
	}
	extra := &request_models.LsConfirmExtra{
		CardInfo:     lsCard,
		SettleConfig: &request_models.SettleInfo{SettleCycle: "D1"},
	}
	confirm := &request_models.LsConfirm{
		MchNo:        l.svcCtx.Config.LsPay.MchNo,
		AppId:        l.svcCtx.Config.LsPay.AppId,
		OrderId:      common.GetStrId(),
		WayCode:      "APPLY",
		AccountNo:    member.LsUser,
		ChannelExtra: extra,
		ReqTime:      common.GetIntId(),
		Version:      "1.0",
		SignType:     "MD5",
	}
	rsp, err := confirm.Confirm(l.svcCtx.Config.LsPay.PayKey)
	if err != nil {
		return nil, err
	}

	if rsp.Status == 200 {
		if len(rsp.Data.AccountNo) > 0 {
			cardInfo.CardId = rsp.Data.AccountNo
			req.CardId = rsp.Data.AccountNo
			_, err = l.svcCtx.Engine.Insert(cardInfo)
			if err != nil {
				return nil, err
			}
			member := &models.Member{LsCard: cardInfo.CardId}
			_, _ = l.svcCtx.Engine.ID(req.UserId).Cols("ls_card").Update(member)
		} else {
			return nil, errors.New(rsp.Data.ErrMsg)
		}
	} else {
		return nil, errors.New(rsp.Msg)
	}
	return req, nil
}
